package com.sm.filemanagelibrary.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.scorpio.baselibrary.ui.BasicFragment;
import com.sm.filemanagelibrary.FCConstant;
import com.sm.filemanagelibrary.FileSelectedActivity;
import com.sm.filemanagelibrary.R;
import com.sm.filemanagelibrary.adapter.FolderLiseAdapter;
import com.sm.filemanagelibrary.model.FileEntity;
import com.sm.filemanagelibrary.model.FolderEntity;
import com.sm.filemanagelibrary.util.FileLoadingUtil;
import com.sm.filemanagelibrary.util.FileManage;
import com.sm.filemanagelibrary.util.OnFileLoadingListener;
import com.sm.filemanagelibrary.view.ImageFilePagerDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * created by zhud on 2019/9/29
 */
public class FileOverallFragment extends BasicFragment implements BaseQuickAdapter.OnItemChildClickListener {

    private FolderLiseAdapter adapter;
    private List<MultiItemEntity> entities = new ArrayList<>();
    private View loadingLayout;
    private View nullLayout;
    private RecyclerView recyclerView;
    private ImageFilePagerDialog filePagerDialog;

    public static FileOverallFragment getFileOverallFragment(int type) {
        FileOverallFragment fragment = new FileOverallFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int onCreateLayout() {
        return R.layout.fragment_file_overall;
    }

    @Override
    protected void onInit(Bundle savedInstanceState) {
        recyclerView = v.getView(R.id.recyclerView);
        v.setRecyclerViewAdapter(R.id.recyclerView, adapter = new FolderLiseAdapter(entities))
                .setRecyclerViewLayoutManager(R.id.recyclerView, getManager());
        loadingLayout = LayoutInflater.from(getContext()).inflate(R.layout.layout_file_loading, null);
        nullLayout = LayoutInflater.from(getContext()).inflate(R.layout.layout_file_null_data, null);
        adapter.setEmptyView(loadingLayout);
        adapter.setOnItemChildClickListener(this);
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        filePagerDialog = new ImageFilePagerDialog(getContext());
        init();
    }

    private void init() {
        int type = getArguments().getInt("type", -1);
        switch (type) {
            case 0://影音
                FileLoadingUtil.loadingPhoneVideo(getContext(), onFileLoadingListener);
                break;
            case 1://图片
                FileLoadingUtil.loadingPhoneImage(getContext(), onFileLoadingListener);
                break;
            case 2://文档
                FileLoadingUtil.loadingPhoneDocument(getContext(), onFileLoadingListener);
                break;
            case 3://应用
                FileLoadingUtil.loadingPhoneApp(getContext(), onFileLoadingListener);
                break;
            case 4://其他
                FileManage.open(getContext());
                onFileLoadingListener.onfileLoading(new ArrayList<FolderEntity>());
                break;
        }
    }

    private GridLayoutManager getManager() {
        final GridLayoutManager manager = new GridLayoutManager(getContext(), 4);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (adapter.getData().size() <= 0) {
                    return manager.getSpanCount();
                }
                MultiItemEntity entity = adapter.getItem(position);
                if (entity.getItemType() == FolderLiseAdapter.TYPE_IMAGE) {
                    return 1;
                }
                return manager.getSpanCount();
            }
        });
        return manager;
    }


    private OnFileLoadingListener onFileLoadingListener = new OnFileLoadingListener() {
        @Override
        public void onfileLoading(List<FolderEntity> foldersEntity) {
            entities.clear();
            entities.addAll(foldersEntity);
            adapter.setEmptyView(nullLayout);
            adapter.notifyDataSetChanged();
        }
    };

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        int id = view.getId();
        FileEntity fileEntity = (FileEntity) adapter.getItem(position);
        if (id == R.id.file_check || id == R.id.file_manage_item_layout) {
            fileEntity.checked = !fileEntity.checked;
            FileSelectedActivity.onSelectedEvent(fileEntity, fileEntity.checked ? FCConstant.SELECTED_FILE_STATE_ADD : FCConstant.SELECTED_FILE_STATE_DEL);
        } else if (id == R.id.item_file_image) {
            List<String> images = new ArrayList<>();
            int showIndex = 0;
            List<MultiItemEntity> entityList = adapter.getData();
            for (MultiItemEntity multiItemEntity : entityList) {
                if (multiItemEntity.getItemType() == FolderLiseAdapter.TYPE_IMAGE) {
                    FileEntity file = (FileEntity) multiItemEntity;
                    images.add(file.path);
                    if (fileEntity == file) {
                        showIndex = images.size() - 1;
                    }
                }
            }
            filePagerDialog.show(images, showIndex);
        }
        adapter.notifyItemChanged(position);
    }
}
